package com.etc.controller;

import com.etc.pojo.News;
import com.etc.pojo.User;
import com.etc.service.NewsDaoServices;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @program: RookieSocialNetworking
 * @description: 动态的控制器类
 * @author: Mr.单
 * @create: 2020-08-15 13:25
 **/
@Controller
@RequestMapping("NewsController")
public class NewsController {
    /**
     * 查看所有动态信息
     */
    @Autowired
    NewsDaoServices services;
    @ResponseBody
    @RequestMapping("queryNews")
    public List<News> queryNews(){
        List<News> list = services.getallNews();
        return  list;
    }
    @ResponseBody
    @RequestMapping("queryNews2")
    public PageInfo<News> queryPageNews(String pn,String ps){
        Integer pageNum = 1;
        Integer pageSize = 3;
        if(pn!=null && !pn.trim().equals("")){
            pageNum = Integer.parseInt(pn);
        }
        if(ps!=null && !ps.trim().equals("")){
            pageSize = Integer.parseInt(ps);
        }
        PageInfo<News> page = services.getPageAllNews(pageNum,pageSize);
        return  page;
    }


    @RequestMapping(value = "addNews",method = RequestMethod.GET)
    @ResponseBody
    public String addNews(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {
        User user =(User) session.getAttribute("user");
        String newsDetail = request.getParameter("content");
        System.out.println("news"+newsDetail);
        String newsAdd = request.getParameter("address");
        System.out.println("newsadd"+newsAdd);
        News news = new News(newsDetail, newsAdd, user.getUserId());
        System.out.println("测试userID"+user.getUserId());

        boolean b = services.pushNews(news);
        return "newsController/queryNews";

    }
    @RequestMapping("deleteNews")
    @ResponseBody
    public boolean deleteNews(int newsId){
      return services.deleteN(newsId);
    }
}
